package cn.vcjf.weapp.api.payment.service;

import cn.vcjf.weapp.api.goods.entity.GoodsRefundOrder;
import cn.vcjf.weapp.api.goods.mapper.GoodsRefundOrderMapper;
import cn.vcjf.weapp.api.payment.qfwxpay.service.AbstractQfRefundService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * @Auther: zhengzihang
 * @Date: 2018/10/16 17:26
 * @Description:
 */
@Service
public class QfRefundServiceImpl extends AbstractQfRefundService<GoodsRefundOrder> {

	@Resource
	private GoodsRefundOrderMapper goodsRefundOrderMapper;

	@Override
	protected BigDecimal getTraAmount(GoodsRefundOrder order) {
		return order.getRefundMoney();
	}

	//退款状态(1,待审核,2,已审核,3,拒绝,4申请成功,5已完成,6失败)
	@Override
	protected long getOrderSuccessStatus(GoodsRefundOrder order) {
		return 4;
	}

	@Override
	protected long getOrderStatus(GoodsRefundOrder order) {
		return order.getStatus();
	}

	@Override
	public List<GoodsRefundOrder> findTOrderByNo(String orderNo) {
		Wrapper<GoodsRefundOrder> query = new EntityWrapper<>();
		query.eq("orderNo", orderNo);

		return goodsRefundOrderMapper.selectList(query);
	}

	@Override
	protected String getPayOrderNo(GoodsRefundOrder order) {
		return order.getOrderNo();
	}

	@Override
	protected String getOrderNo(GoodsRefundOrder order) {
		return order.getOrderNo();
	}

	@Override
	protected int doRefundSuccess(GoodsRefundOrder order, JSONObject ressultJson) {
		order.setAllRes(ressultJson.toJSONString());
		order.setStatus(4);
		order.setOrderDes("退款申请成功");
		order.setOrderSn(ressultJson.getString("syssn"));

		return goodsRefundOrderMapper.updateById(order);
	}

	@Override
	protected int doRefundFailed(GoodsRefundOrder order, JSONObject ressultJson) {
		order.setAllRes(ressultJson.toJSONString());
		order.setStatus(6);
		order.setOrderDes("退款申请失败:" + ressultJson.getString("resperr"));

		return goodsRefundOrderMapper.updateById(order);
	}
}
